package com.china08.yxyapi.util;

import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.SystemConstant;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongLectureSchoolStatistics;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultSchool;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultTea;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultSchoolRepository;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultTeaRepository;
import com.china08.yxyapi.repository.db.statistics.YxyHuodongLectureSchoolStatisticsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@Component
public class LectureUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(LectureUtil.class);

    @Autowired
    private YxyHuodongLectureSchoolStatisticsRepository repository;

    @Autowired
    private MongoTemplate mt;

    @Autowired
    private YxyHuodongLectureResultSchoolRepository schoolRepository;

    @Autowired
    private YxyHuodongLectureResultTeaRepository teaRepository;

    /**
     * 5 代表审核上架下架 status
     * 
     * @param profile
     * @param status
     */
    public void save(YxyProfile profile, Integer status, String aread, String schoolId, java.util.Date createdDate) {
        LOGGER.info("开始统计数据.....................................");
        // 必须是哈尔滨市 下的学校
        if (StringUtils.equals("230100", profile.getSchoolAreaIdCity()) || "230100".equals(aread)) {
            LOGGER.info("哈尔滨下的学校用处理");

            // 2017 - 05 -31
            // 包含当天 开始时间
            LocalDate date = LocalDate.of(2017, 6, 12);
            // 不包含当天
            LocalDate date1 = LocalDate.of(2017, 10, 1);
            // 线上

            if (createdDate == null) {
                createdDate = new java.util.Date();
            }
            Instant instant = createdDate.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate now = instant.atZone(zoneId).toLocalDate();

            // 5/31
            date = LocalDate.of(2017, 6, 11);

            if (now.isAfter(date) && now.isBefore(date1)) {
                // 6/1
                date = LocalDate.of(2017, 6, 12);
                LOGGER.info("在活动期间:" + now);
            } else {
                LOGGER.info("不在活动期间:" + now);
                return;
            }

            Date date2 = Date.valueOf(date1);
            Date date3 = Date.valueOf(date);
            String sid = profile.getSchoolId();
            if (5 == status) {
                sid = schoolId;
            }
            YxyHuodongLectureSchoolStatistics schoolStatistics = repository.findBySchoolId(sid);

            Criteria criteria = Criteria.where("schoolId").is(sid).and("createdDate").gt(date3).lt(date2).and("share")
                    .is(1).and("status").is(1);// and("isDelete").is(0)
            long c = mt.count(new Query(criteria), YxyLecturePrePack.class);
            if (c == 0) {
                if (schoolStatistics != null) {
                    repository.delete(schoolStatistics);
                }
                return;
            }
            if (schoolStatistics == null) {
                schoolStatistics = new YxyHuodongLectureSchoolStatistics();
                schoolStatistics.setSchoolId(sid);
                schoolStatistics.setSchoolNick(profile.getSchoolName());
                schoolStatistics.setAddress(profile.getSchoolAddress());
                schoolStatistics.setAreaIdProvince("230000");
                schoolStatistics.setAreaNameProvince("黑龙江省");
                schoolStatistics.setAreaIdCity("230100");
                schoolStatistics.setAreaNameCity("哈尔滨市");
                schoolStatistics.setAreaIdDistrict(profile.getSchoolAreaIdDistrict());
                int index = Arrays.asList(SystemConstant.HAERBINCODE).indexOf(profile.getSchoolAreaIdDistrict());
                if (index > -1) {
                    schoolStatistics.setAreaNameDistrict(SystemConstant.HAERBINNAME[index]);
                } else {
                    LOGGER.error(
                            "学校地区编码错误 请查看,学校:" + profile.getSchoolId() + "地区编码:" + profile.getSchoolAreaIdDistrict());
                    return;
                }
                schoolStatistics.setPackCount(c);
                LOGGER.info("新增学校.....................................");
            } else {
                schoolStatistics.setPackCount(c);
                LOGGER.info("更新学校.....................................");
            }
            repository.saveAndFlush(schoolStatistics);
            LOGGER.info("更新完成.....................................");
        } else {
            LOGGER.info("不是哈尔滨下查看是否是合山下的学校");
            saveHs(profile, status, aread, schoolId, createdDate);
        }

    }

    public void saveHs(YxyProfile profile, Integer status, String aread, String schoolId, java.util.Date createdDate) {
        LOGGER.info("开始统计数据.....................................");
        // 必须是合山下的学校
        if (StringUtils.equals("451381", profile.getSchoolAreaIdDistrict()) || "451381".equals(aread)) {
            // 2017 - 07 -10
            // 包含当天 开始时间
            LocalDate date = LocalDate.of(2017, 7, 10);
            // 结束
            // 不包含当天
            LocalDate date1 = LocalDate.of(2017, 9, 16);
            // 线上

            if (createdDate == null) {
                createdDate = new java.util.Date();
            }
            Instant instant = createdDate.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate now = instant.atZone(zoneId).toLocalDate();

            // 前一天
            date = LocalDate.of(2017, 7, 9);

            if (now.isAfter(date) && now.isBefore(date1)) {
                // 6/1
                date = LocalDate.of(2017, 7, 10);
                LOGGER.info("在活动期间:" + now);
            } else {
                LOGGER.info("不在活动期间:" + now);
                return;
            }

            Date date2 = Date.valueOf(date1);
            Date date3 = Date.valueOf(date);
            String sid = profile.getSchoolId();
            if (5 == status) {
                sid = schoolId;
            }
            YxyHuodongLectureSchoolStatistics schoolStatistics = repository.findBySchoolId(sid);

            Criteria criteria = Criteria.where("schoolId").is(sid).and("createdDate").gt(date3).lt(date2).and("share")
                    .is(1).and("status").is(1);// and("isDelete").is(0)
            long c = mt.count(new Query(criteria), YxyLecturePrePack.class);
            if (c == 0) {
                if (schoolStatistics != null) {
                    repository.delete(schoolStatistics);
                }
                return;
            }
            if (schoolStatistics == null) {
                schoolStatistics = new YxyHuodongLectureSchoolStatistics();
                schoolStatistics.setSchoolId(sid);
                schoolStatistics.setSchoolNick(profile.getSchoolName());
                schoolStatistics.setAddress(profile.getSchoolAddress());
                schoolStatistics.setAreaIdProvince("450000");
                schoolStatistics.setAreaNameProvince("广西壮族自治区");
                schoolStatistics.setAreaIdCity("451300");
                schoolStatistics.setAreaNameCity("来宾市");
                schoolStatistics.setAreaIdDistrict("451381");
                schoolStatistics.setAreaNameDistrict("合山市");
                schoolStatistics.setPackCount(c);
                LOGGER.info("新增学校.....................................");
            } else {
                schoolStatistics.setPackCount(c);
                LOGGER.info("更新学校.....................................");
            }
            repository.saveAndFlush(schoolStatistics);
            LOGGER.info("更新完成.....................................");
        } else {
            LOGGER.info("不是合山市的学校不用处理");
        }

    }

    /**
     * 为了不影响原有的逻辑 saveYt没有重名名 它是一个入口
     * 
     * @param aread
     * @param schoolId
     * @param userId
     * @param userName
     * @param schoolName
     */
    // 活动入口
    public void saveYt(String aread, String schoolId, String userId, String userName, String schoolName,
            java.util.Date createdDate) {
        LOGGER.info("开始统计数据.....................................");
        if (createdDate == null) {
            createdDate = new java.util.Date();
        }
        // 印台
        if ("610203".equals(aread)) {
            YtActivity(aread, schoolId, userId, userName, schoolName, createdDate);
            // 金沙县
        } else if ("520523".equals(aread)) {
            JsActivity(aread, schoolId, userId, userName, schoolName, createdDate);
            // 剑河县
        } else if ("522629".equals(aread)) {
            JhsActivity(aread, schoolId, userId, userName, schoolName, createdDate);
        } else if ("320923".equals(aread)) {
            Fntivity(aread, schoolId, userId, userName, schoolName, createdDate);
        } else {
            LOGGER.info("不在活动区县内");
        }
    }

    // 必须是剑河县
    private void JhsActivity(String aread, String schoolId, String userId, String userName, String schoolName,
            java.util.Date createdDate) {

        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 12, 5);
        // 结束
        // 不包含当天 比如结束时间是:2017-11-25 此处写: 2017-11-16
        LocalDate end = LocalDate.of(2018, 3, 31);

        LocalDate startYesterday = start.minusDays(1);
        // 线上
        Instant instant = createdDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate now = instant.atZone(zoneId).toLocalDate();
        if (now.isAfter(startYesterday) && now.isBefore(end)) {
            LOGGER.info("在活动期间:" + now);
        } else {
            LOGGER.info("不在活动期间:" + now);
            return;
        }
        Date date2 = Date.valueOf(end);

        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("520000");
            lectureResultTea.setAreaNameProvince("贵州省");
            lectureResultTea.setAreaIdCity("522600");
            lectureResultTea.setAreaNameCity("黔东南苗族侗族自治州");
            lectureResultTea.setAreaIdDistrict("522629");
            lectureResultTea.setAreaNameDistrict("剑河县");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("520000");
            resultSchool.setAreaNameProvince("贵州省");
            resultSchool.setAreaIdCity("522600");
            resultSchool.setAreaNameCity("黔东南苗族侗族自治州");
            resultSchool.setAreaIdDistrict("522629");
            resultSchool.setAreaNameDistrict("剑河县");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }

    // 必须是金沙县的学校
    private void JsActivity(String aread, String schoolId, String userId, String userName, String schoolName,
            java.util.Date createdDate) {

        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 11, 22);
        // 结束
        // 不包含当天 比如结束时间是:2017-11-25 此处写: 2017-11-16
        LocalDate end = LocalDate.of(2018, 1, 1);

        LocalDate startYesterday = start.minusDays(1);
        // 线上
        Instant instant = createdDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate now = instant.atZone(zoneId).toLocalDate();

        if (now.isAfter(startYesterday) && now.isBefore(end)) {
            LOGGER.info("在活动期间:" + now);
        } else {
            LOGGER.info("不在活动期间:" + now);
            return;
        }

        Date date2 = Date.valueOf(end);

        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("520000");
            lectureResultTea.setAreaNameProvince("贵州省");
            lectureResultTea.setAreaIdCity("520500");
            lectureResultTea.setAreaNameCity("毕节市");
            lectureResultTea.setAreaIdDistrict("520523");
            lectureResultTea.setAreaNameDistrict("金沙县");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("520000");
            resultSchool.setAreaNameProvince("贵州省");
            resultSchool.setAreaIdCity("520500");
            resultSchool.setAreaNameCity("毕节市");
            resultSchool.setAreaIdDistrict("520523");
            resultSchool.setAreaNameDistrict("金沙县");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }

    // 必须是合山下的学校
    private void YtActivity(String aread, String schoolId, String userId, String userName, String schoolName,
            java.util.Date createdDate) {
        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 11, 15);
        // 结束
        // 不包含当天 比如结束时间是:2017-11-25 此处写: 2017-11-16
        LocalDate end = LocalDate.of(2018, 1, 1);

        LocalDate startYesterday = start.minusDays(1);
        // 线上
        Instant instant = createdDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate now = instant.atZone(zoneId).toLocalDate();
        if (now.isAfter(startYesterday) && now.isBefore(end)) {
            LOGGER.info("在活动期间:" + now);
        } else {
            LOGGER.info("不在活动期间:" + now);
            return;
        }

        Date date2 = Date.valueOf(end);

        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("610000");
            lectureResultTea.setAreaNameProvince("陕西省");
            lectureResultTea.setAreaIdCity("610200");
            lectureResultTea.setAreaNameCity("铜川市");
            lectureResultTea.setAreaIdDistrict("610203");
            lectureResultTea.setAreaNameDistrict("印台区");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("610000");
            resultSchool.setAreaNameProvince("陕西省");
            resultSchool.setAreaIdCity("610200");
            resultSchool.setAreaNameCity("铜川市");
            resultSchool.setAreaIdDistrict("610203");
            resultSchool.setAreaNameDistrict("印台区");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }

    // 必须是阜宁县的学校
    private void Fntivity(String aread, String schoolId, String userId, String userName, String schoolName,
            java.util.Date createdDate) {
        // 包含当天 开始时间
        LocalDate start = LocalDate.of(2017, 11, 15);
        // 结束
        // 不包含当天 比如结束时间是:2017-11-25 此处写: 2017-11-16
        LocalDate end = LocalDate.of(2018, 1, 1);

        LocalDate startYesterday = start.minusDays(1);
        // 线上
        Instant instant = createdDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate now = instant.atZone(zoneId).toLocalDate();
        if (now.isAfter(startYesterday) && now.isBefore(end)) {
            LOGGER.info("在活动期间:" + now);
        } else {
            LOGGER.info("不在活动期间:" + now);
            return;
        }
        Date date2 = Date.valueOf(end);

        Date date3 = Date.valueOf(start);

        Criteria criteria = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("auditStats").is(1);
        long c = mt.count(new Query(criteria), YxyLecturePrePack.class);

        YxyHuodongLectureResultSchool resultSchool = schoolRepository.findBySchoolId(schoolId);
        List<YxyHuodongLectureResultTea> list2 = teaRepository.findBySchoolId(schoolId);
        if (c == 0) {
            if (resultSchool != null) {
                schoolRepository.delete(resultSchool);
            }
            if (CollectionUtils.isNotEmpty(list2)) {
                teaRepository.delete(list2);
            }
            return;
        }

        YxyHuodongLectureResultTea lectureResultTea = teaRepository.findBySchoolIdAndTeaId(schoolId, userId);

        Criteria criteria1 = Criteria.where("schoolId").is(schoolId).and("createdDate").gt(date3).lt(date2).and("share")
                .is(1).and("status").is(1).and("userId").is(userId).and("auditStats").is(1);
        long c1 = mt.count(new Query(criteria1), YxyLecturePrePack.class);

        if (c1 == 0) {
            if (lectureResultTea != null) {
                teaRepository.delete(lectureResultTea);
            }
            return;
        }
        if (lectureResultTea == null) {
            lectureResultTea = new YxyHuodongLectureResultTea();
            lectureResultTea.setTeaId(userId);
            lectureResultTea.setTeaName(userName);
            lectureResultTea.setSchoolId(schoolId);
            lectureResultTea.setSchoolNick(schoolName);
            lectureResultTea.setAreaIdProvince("320000");
            lectureResultTea.setAreaNameProvince("江苏省");
            lectureResultTea.setAreaIdCity("320900");
            lectureResultTea.setAreaNameCity("盐城市");
            lectureResultTea.setAreaIdDistrict("320923");
            lectureResultTea.setAreaNameDistrict("阜宁县");
            lectureResultTea.setPackCount(c1);
            LOGGER.info("新增老师.....................................");
        } else {
            lectureResultTea.setPackCount(c1);
            LOGGER.info("更新老师.....................................");
        }
        lectureResultTea = teaRepository.save(lectureResultTea);
        LOGGER.info("更新老师数量完成.....................................");

        Integer count = teaRepository.countBySchoolId(schoolId);
        if (resultSchool == null) {
            resultSchool = new YxyHuodongLectureResultSchool();
            resultSchool.setSchoolId(schoolId);
            resultSchool.setSchoolNick(schoolName);
            resultSchool.setAreaIdProvince("320000");
            resultSchool.setAreaNameProvince("江苏省");
            resultSchool.setAreaIdCity("320900");
            resultSchool.setAreaNameCity("盐城市");
            resultSchool.setAreaIdDistrict("320923");
            resultSchool.setAreaNameDistrict("阜宁县");
            resultSchool.setPackCount(c);
            LOGGER.info("新增学校.....................................");
        } else {
            resultSchool.setPackCount(c);
            LOGGER.info("更新学校.....................................");
        }
        if (count == null) {
            count = 0;
        }
        resultSchool.setTeaCount(count);
        schoolRepository.save(resultSchool);
        LOGGER.info("更新学校数量完成.....................................");
    }
}
